package top.tangyh.lamp.care.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.tangyh.basic.base.R;
import top.tangyh.basic.interfaces.echo.EchoService;
import top.tangyh.basic.base.controller.SuperController;
import top.tangyh.lamp.base.entity.user.BaseEmployee;
import top.tangyh.lamp.base.service.user.BaseEmployeeService;
import top.tangyh.lamp.care.service.BPgRecordService;
import top.tangyh.lamp.care.entity.BPgRecord;
import top.tangyh.lamp.care.vo.query.PgRecordInVO;
import top.tangyh.lamp.care.vo.query.PgRecordQueryVo;
import top.tangyh.lamp.care.vo.result.PgRecordOutVO;
import top.tangyh.lamp.care.vo.save.BPgRecordSaveVO;
import top.tangyh.lamp.care.vo.save.PgRecordVo;
import top.tangyh.lamp.care.vo.update.BPgRecordUpdateVO;
import top.tangyh.lamp.care.vo.result.BPgRecordResultVO;
import top.tangyh.lamp.care.vo.query.BPgRecordPageQuery;
import top.tangyh.lamp.system.entity.tenant.DefUser;
import top.tangyh.lamp.system.service.tenant.DefUserService;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * 评估评分
 * </p>
 *
 * @author zqb
 * @date 2024-07-09 08:35:22
 * @create [2024-07-09 08:35:22] [zqb]
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/pgrecord")
@Tag(name = "评估评分-评估记录")
public class BPgRecordController {

    //    extends SuperController<BPgRecordService, Long, BPgRecord
//            , BPgRecordSaveVO, BPgRecordUpdateVO, BPgRecordPageQuery, BPgRecordResultVO>
//    private final EchoService echoService;
//    @Override
//    public EchoService getEchoService() {
//        return echoService;
//    }
    private final DefUserService defUserService;

    private final BPgRecordService bPgRecordService;

    @Operation(summary = "保存评估记录")
    @PostMapping("/add")
    public R create(@Validated @RequestBody PgRecordVo pgRecordVo) {
        Boolean save = bPgRecordService.savePgRecordVo(pgRecordVo);
        return R.success(save);
    }

    @Operation(summary = "查询评估记录,根据病人、日期、排班查询-护理记录模块需要调用")
    @PostMapping("/sel/list")
    public R<List<BPgRecordResultVO>> query(@RequestBody PgRecordQueryVo criteria) {


        List<BPgRecordResultVO> list = bPgRecordService.selectByPid(criteria);
        if(null!=list){
            for (int i = 0; i < list.size(); i++) {
                BPgRecordResultVO bPgRecordResultVO = list.get(i);
                DefUser defUser = defUserService.getById(bPgRecordResultVO.getCreatedBy());
                if (null != defUser) {
                    bPgRecordResultVO.setPfUserName(defUser.getNickName());
                }
            }
        }

        return R.success(list);
    }


    @Operation(summary = " 查询评估记录,根据病人住院号、评估id")
    @PostMapping("/queryNewest")
    public R<PgRecordVo> query3(@RequestBody PgRecordQueryVo criteria) {
        return R.success(bPgRecordService.selectByHp(criteria));
    }

    @Operation(summary = "查询评估记录,根据评估记录")
    @GetMapping("/queryById")
    public R<PgRecordVo> query2(@RequestParam Long id) {
        return R.success(bPgRecordService.selectByRecordId(id));
    }

    @Operation(summary = "查询评估记录,根据评估记录")
    @PostMapping("/list")
    public R<List<PgRecordVo>> queryListByIds(@RequestBody List<Long> ids) {
        return R.success(bPgRecordService.selectByRecordIds(ids));
    }


    @Operation(summary = "删除评估记录")
    @DeleteMapping("/deleteById")
    public R delete(@RequestParam("id") Long id) {
        if (null != id) {
            BPgRecord old = bPgRecordService.getById(id);
//            if(null != SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy())){
//                throw new BadRequestException(SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy()));
//            }

        }
        bPgRecordService.delete(id);
        return R.success();
    }

    @Operation(summary = "查询最新一次入科后的第一条疼痛评分记录")
    @GetMapping("/first/pain")
    public R<PgRecordVo> getFirstRecodOfZK(String hospitalNumber) {
        return R.success(bPgRecordService.getFirstOfZk(hospitalNumber));
    }

    @Operation(summary = "仅查询评估记录信息")
    @GetMapping("/sel/master")
    public R<List<PgRecordOutVO>> queryOnryRecord(PgRecordInVO param) {
        return R.success(bPgRecordService.queryOnryRecord(param));
    }
}


